//
//  LogSheetView.m
//  StationManager
//
//  Created by 张佳强 on 18/5/12.
//  Copyright © 2018年 Helong. All rights reserved.
//

#import "LogSheetView.h"
#import <Masonry.h>
#import "SheetCollectionViewCell.h"
#import "SheetImageCell.h"
#import <UIImageView+WebCache.h>

@implementation LogSheetView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame{
    
    
    self = [super initWithFrame:frame];
    if (self) {
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"区域自动气象站月维护记录表";
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.right.equalTo(@0);
            make.height.mas_equalTo(30);
        }];
    }
    return self;
}

-(void)setSheetView{
    
    
    if (_firstColloectionView) {
        
        [_firstColloectionView removeFromSuperview];
    }
    
    if (_bottomColloectionView) {
        
        [_bottomColloectionView removeFromSuperview];
    }
    
    if (_sideColloectionView) {
        
        [_sideColloectionView removeFromSuperview];
    }
    
    if (_middleColloectionView) {
        
        [_middleColloectionView removeFromSuperview];
    }
    
//    _firstArray = @[@"台站名称",@"",@"台站号",@"",@"维护日期",@"",@"维护人员",@"",@"运维项目",@"先前状态",@"运维措施",@"运维结果"];
//    _sideArray = @[@"供电情况",@"暴雨站设备",@"其他"];
    _bottomArray = @[@"备注",_remark,@""];
    _middleArray = [NSMutableArray arrayWithCapacity:0];
//    [_middleArray addObjectsFromArray:@[@"太阳能板",@"",@"",@"",@"蓄电池",@"",@"",@"",@"电缆",@"",@"",@"",@"雨量筒",@"",@"",@"",@"雨量传感器",@"",@"",@"",@"采集器",@"",@"",@"",@"运维环境",@"",@"",@""]];
    
    for (NSString *keys in _sideArray) {
        [_middleArray addObjectsFromArray:_dataDict[keys]];
    }
    NSLog(@"asdasdads:%@",_middleArray);
    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc]init];
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    _firstColloectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 30, SCREEN_WIDTH-20, 94)collectionViewLayout:flow];
    _firstColloectionView.backgroundColor = [UIColor lightGrayColor];
    _firstColloectionView.delegate = self;
    _firstColloectionView.dataSource = self;
    _firstColloectionView.bounces = YES;
    [_firstColloectionView registerClass:[SheetCollectionViewCell class] forCellWithReuseIdentifier:@"firstCell"];

    [self addSubview:_firstColloectionView];
    
    UICollectionViewFlowLayout * flow2 = [[UICollectionViewFlowLayout alloc]init];
    flow2.scrollDirection = UICollectionViewScrollDirectionVertical;
    _sideColloectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 94+30, 22, _middleArray.count/4*32)collectionViewLayout:flow2];
    _sideColloectionView.backgroundColor = [UIColor lightGrayColor];
    _sideColloectionView.delegate = self;
    _sideColloectionView.dataSource = self;
    _sideColloectionView.bounces = YES;
    [_sideColloectionView registerClass:[SheetCollectionViewCell class] forCellWithReuseIdentifier:@"sideCell"];
    
    [self addSubview:_sideColloectionView];
    
    UICollectionViewFlowLayout * flow3 = [[UICollectionViewFlowLayout alloc]init];
    flow3.scrollDirection = UICollectionViewScrollDirectionVertical;
    _middleColloectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(32, 94+30, SCREEN_WIDTH-42, _middleArray.count/4*32)collectionViewLayout:flow3];
    _middleColloectionView.backgroundColor = [UIColor lightGrayColor];
    _middleColloectionView.delegate = self;
    _middleColloectionView.dataSource = self;
    _middleColloectionView.bounces = YES;
    [_middleColloectionView registerClass:[SheetCollectionViewCell class] forCellWithReuseIdentifier:@"middleCell"];
    
    [self addSubview:_middleColloectionView];
    
    UICollectionViewFlowLayout * flow4 = [[UICollectionViewFlowLayout alloc]init];
    flow4.scrollDirection = UICollectionViewScrollDirectionVertical;
    _bottomColloectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 94+30+_middleArray.count/4*32, SCREEN_WIDTH-20, 302)collectionViewLayout:flow4];
    _bottomColloectionView.backgroundColor = [UIColor lightGrayColor];
    _bottomColloectionView.delegate = self;
    _bottomColloectionView.dataSource = self;
    _bottomColloectionView.bounces = YES;
    [_bottomColloectionView registerClass:[SheetCollectionViewCell class] forCellWithReuseIdentifier:@"bottomCell"];
    [_bottomColloectionView registerClass:[SheetImageCell class] forCellWithReuseIdentifier:@"imageCell"];
    
    [self addSubview:_bottomColloectionView];


}

#pragma UICollectionView - delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == _firstColloectionView) {
        return _firstArray.count;

    }
    else if(collectionView == _sideColloectionView){
        return _sideArray.count;
    }
    else if(collectionView == _middleColloectionView){
        return _middleArray.count;
    }
    else{
        return _bottomArray.count;
    }
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 1;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 1;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (collectionView == _firstColloectionView) {
        return UIEdgeInsetsMake(1, 1, 1, 1);

    }
    else if(collectionView == _sideColloectionView){
        return UIEdgeInsetsMake(0, 1, 1, 0);

    }
    else if(collectionView == _middleColloectionView){
        
        return UIEdgeInsetsMake(0, 1, 1, 1);
    }
    else{
       return UIEdgeInsetsMake(0, 1, 1, 1);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == _firstColloectionView) {
        
        if (indexPath.item == 0) {
            return CGSizeMake((collectionView.frame.size.width-5)/4-20, 30);
        }
        else if (indexPath.item == 1){
            return CGSizeMake((collectionView.frame.size.width-5)/4+20, 30);
        }
        else if (indexPath.item == 2){
            return CGSizeMake((collectionView.frame.size.width-5)/4-20, 30);
        }
        else if (indexPath.item == 3){
            return CGSizeMake((collectionView.frame.size.width-5)/4+20, 30);
        }
        else if (indexPath.item == 4) {
            return CGSizeMake((collectionView.frame.size.width-5)/4-20, 30);
        }
        else if (indexPath.item == 5){
            return CGSizeMake((collectionView.frame.size.width-5)/4+20, 30);
        }
        else if (indexPath.item == 6){
            return CGSizeMake((collectionView.frame.size.width-5)/4-20, 30);
        }
        else if (indexPath.item == 7){
            return CGSizeMake((collectionView.frame.size.width-5)/4+20, 30);
        }
        else if (indexPath.item == 8) {
            return CGSizeMake((collectionView.frame.size.width-5)/4, 30);
        }
        else if (indexPath.item == 9){
            return CGSizeMake((collectionView.frame.size.width-5)/4, 30);
        }
        else if (indexPath.item == 10){
            return CGSizeMake((collectionView.frame.size.width-5)/4, 30);
        }
        else{
            return CGSizeMake((collectionView.frame.size.width-5)/4, 30);
        }

    }
    else if(collectionView == _sideColloectionView){
        
        _middleSecondArray = _dataDict[@"其他"];
        _middleFirstArray = _dataDict[@"供电情况"];
        NSInteger row1 = _middleArray.count/4;
        NSInteger row2 = _middleArray.count/4;
//        总行数
        NSInteger row = _middleArray.count/4;
        if ([_sideArray[0] isEqualToString:@"供电情况"]) {
            
            row1 = _middleFirstArray.count/4;
            row2 = (_middleArray.count-_middleFirstArray.count-_middleSecondArray.count)/4;
            if (indexPath.item == 0) {
                
                return CGSizeMake(20, (collectionView.frame.size.height-3)*row2/row);
            }
            else if (indexPath.item == 1){
                
                return CGSizeMake(20, (collectionView.frame.size.height-3)*row1/row);
            }
            else{
                return CGSizeMake(20, (collectionView.frame.size.height-3)/row);
            }
        }
        else{
            row2 = _middleFirstArray.count/4;
            row1 = (_middleArray.count-_middleFirstArray.count-_middleSecondArray.count)/4;
            
            if (indexPath.item == 0) {
                
                return CGSizeMake(20, (collectionView.frame.size.height-3)*row1/row);
            }
            else if (indexPath.item == 1){
                
                return CGSizeMake(20, (collectionView.frame.size.height-3)*row2/row);
            }
            else{
                return CGSizeMake(20, (collectionView.frame.size.height-3)/row);
            }
        }
       
//        NSInteger scale = (1-1/rows)/2;
//        NSLog(@"rows:%zi,%zi",rows,scale);
       
    }
    else if(collectionView == _middleColloectionView){
        
        if (indexPath.item%4 == 0) {
            return CGSizeMake((collectionView.frame.size.width-5+20)/4-20, 31);
        }
        else if (indexPath.item%4 == 1){
            
            return CGSizeMake((collectionView.frame.size.width-5+20)/4, 31);
        }
        else if (indexPath.item%4 == 2){
            
            return CGSizeMake((collectionView.frame.size.width-5+20)/4, 31);
        }
        else {
            
            return CGSizeMake((collectionView.frame.size.width-5+20)/4, 31);
        }
    }
    else{
        
        if (indexPath.item == 0) {
            
             return CGSizeMake((collectionView.frame.size.width-3)/4-20, 30);
        }
        else if (indexPath.item == 1){
            
            return CGSizeMake((collectionView.frame.size.width-3)*3/4+20, 30);
        }
        else{
            
            return CGSizeMake(collectionView.frame.size.width-2, 270);
        }
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == _firstColloectionView) {
        SheetCollectionViewCell * cell = (SheetCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
        cell.dataLabel.text = self.firstArray[indexPath.row];
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }
    else if(collectionView == _sideColloectionView){
        SheetCollectionViewCell * cell = (SheetCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"sideCell" forIndexPath:indexPath];
        cell.dataLabel.text = self.sideArray[indexPath.row];
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
        
    }
    else if(collectionView == _middleColloectionView){
        SheetCollectionViewCell * cell = (SheetCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"middleCell" forIndexPath:indexPath];
        cell.dataLabel.text = self.middleArray[indexPath.row];
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
        
    }
    else{
        
        if (indexPath.item == 2) {
            SheetImageCell * cell = (SheetImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
            [cell.logImageView sd_setImageWithURL:[NSURL URLWithString:_imageUrl]];
            cell.backgroundColor = [UIColor whiteColor];
            return cell;
        }
        else{
            SheetCollectionViewCell * cell = (SheetCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"bottomCell" forIndexPath:indexPath];
            cell.dataLabel.text = self.bottomArray[indexPath.row];
            cell.backgroundColor = [UIColor whiteColor];
            return cell;
        }
       
    }
   
}





@end
